<template>
  <div class="ocr-test">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>OCR文字识别测试</span>
      </div>
      
      <div class="upload-section">
        <el-upload
          class="upload-demo"
          drag
          :action="uploadUrl"
          :before-upload="beforeUpload"
          :on-success="handleSuccess"
          :on-error="handleError"
          :show-file-list="false"
          accept="image/*">
          <i class="el-icon-upload"></i>
          <div class="el-upload__text">将图片拖到此处，或<em>点击上传</em></div>
          <div class="el-upload__tip" slot="tip">只能上传jpg/png/gif/bmp格式的图片文件，且不超过4MB</div>
        </el-upload>
      </div>

      <div v-if="loading" class="loading-section">
        <el-alert
          title="正在识别图片中的文字，请稍候..."
          type="info"
          :closable="false"
          show-icon>
        </el-alert>
      </div>

      <div v-if="recognizedText" class="result-section">
        <h3>识别结果：</h3>
        <el-input
          type="textarea"
          :rows="10"
          v-model="recognizedText"
          readonly
          placeholder="识别出的文字将显示在这里">
        </el-input>
        
        <div class="result-actions">
          <el-button @click="copyText" type="primary" icon="el-icon-document-copy">
            复制文字
          </el-button>
          <el-button @click="clearResult" type="default" icon="el-icon-delete">
            清空结果
          </el-button>
        </div>
      </div>
    </el-card>
  </div>
</template>

<script>
export default {
  name: 'OCRTest',
  data() {
    return {
      uploadUrl: '/ocr/recognize',
      recognizedText: '',
      loading: false
    }
  },
  methods: {
    beforeUpload(file) {
      // 检查文件类型
      const isImage = file.type.startsWith('image/');
      if (!isImage) {
        this.$message.error('只能上传图片文件！');
        return false;
      }
      
      // 检查文件大小
      const isLt4M = file.size / 1024 / 1024 < 4;
      if (!isLt4M) {
        this.$message.error('上传图片大小不能超过 4MB!');
        return false;
      }
      
      this.loading = true;
      this.recognizedText = '';
      return true;
    },
    
    handleSuccess(response, file) {
      this.loading = false;
      if (response.code === 200) {
        this.recognizedText = response.data || '未识别到文字内容';
        this.$message.success('OCR识别成功！');
      } else {
        this.$message.error(response.message || 'OCR识别失败');
      }
    },
    
    handleError(err, file) {
      this.loading = false;
      console.error('OCR识别错误:', err);
      this.$message.error('OCR识别失败，请检查网络连接和API配置');
    },
    
    copyText() {
      if (!this.recognizedText) {
        this.$message.warning('没有可复制的文字');
        return;
      }
      
      // 创建临时textarea元素来复制文字
      const textarea = document.createElement('textarea');
      textarea.value = this.recognizedText;
      document.body.appendChild(textarea);
      textarea.select();
      document.execCommand('copy');
      document.body.removeChild(textarea);
      
      this.$message.success('文字已复制到剪贴板');
    },
    
    clearResult() {
      this.recognizedText = '';
      this.$message.info('已清空识别结果');
    }
  }
}
</script>

<style scoped>
.ocr-test {
  padding: 20px;
}

.box-card {
  max-width: 800px;
  margin: 0 auto;
}

.upload-section {
  margin-bottom: 20px;
}

.loading-section {
  margin: 20px 0;
}

.result-section {
  margin-top: 20px;
}

.result-section h3 {
  margin-bottom: 10px;
  color: #303133;
}

.result-actions {
  margin-top: 15px;
  text-align: right;
}

.result-actions .el-button {
  margin-left: 10px;
}

.upload-demo {
  text-align: center;
}

.el-upload__tip {
  color: #606266;
  font-size: 12px;
  margin-top: 7px;
}
</style> 